package controller;

import java.io.File;
import java.util.UUID;

import com.jfinal.core.Controller;
import com.jfinal.core.NotAction;
import com.jfinal.upload.UploadFile;

import utils.smartEnv;


//鍒朵綔涓�涓鑹茬殑鍩虹被锛屾湁瑙掕壊鏉冮檺鎺у埗鐨勯兘浠庢绫绘淳鐢�
public class RoleController extends Controller{
	private int role = 0;
	
	//鍑犱釜瑙掕壊甯搁噺鐨勫畾涔�   杩欓噷瀹氫箟鍜屾暟鎹簱鏄噸澶嶇殑锛屽苟闇�瑕佷繚鎸佷竴鑷达紝鍒嗗埆鏇存柊
	static public int SYSADMIN = 4; //系统管理
	static public int DATADMIN = 3; //数据管理
	static public int PROUSER = 2; //oa专业用户
	static public int NORUSER = 1; //oa普通用户
	static public int UNUSE = 0; //禁用
	static public int LOGADMIN = 5; //日志管理

	public int getRole() {
		return role;
	}

	public void setRole(int role) {
		this.role = role;
	}
	
	//鍏朵粬閫氱敤鏂囦欢涓婁紶绛夊嚱鏁�
	@NotAction
	public String upImageFile(String subpath) {
		//   鍙�冭檻鍙﹀涓�涓紝鑷姩澧炲姞鍒朵綔缂╃暐鍥惧姛鑳界殑鍥剧墖涓婁紶鍔熻兘锛岀缉鐣ュ浘鍥哄畾澶у皬
		//subpath鏄浉瀵箄pload鐨勫瓙鏂囦欢澶�, 杩斿洖鍊间负閲嶅懡鍚嶅悗鐨勫浘鐗囨枃浠跺悕
		UploadFile upfile = getFile();
		//璺緞
		String newPath = smartEnv.fileloc + subpath;
		
		//濡傛灉鏂囦欢澶逛笉瀛樺湪鍒欏垱寤�  
		File file =new File(newPath);  
		if  (!file.exists())    
		{    
		    file.mkdir();  
		}
		
		String newFileName = upfile.getFileName();
		//鎵╁睍鍚�
		newFileName = UUID.randomUUID().toString().replaceAll("-", "") + newFileName.substring(newFileName.length()-4);
		
		String newfullname;
		newfullname = newPath + "\\" + newFileName;
		//System.out.println(newfullname);
		upfile.getFile().renameTo(new File(newfullname));
		
		return subpath + "\\" + newFileName;
	}
	@NotAction
	public String upGltfFile(String subpath) {
		//   鍙�冭檻鍙﹀涓�涓紝鑷姩澧炲姞鍒朵綔缂╃暐鍥惧姛鑳界殑鍥剧墖涓婁紶鍔熻兘锛岀缉鐣ュ浘鍥哄畾澶у皬
		//subpath鏄浉瀵箄pload鐨勫瓙鏂囦欢澶�, 杩斿洖鍊间负閲嶅懡鍚嶅悗鐨勫浘鐗囨枃浠跺悕
		UploadFile upfile = getFile();
		//璺緞
		String newPath = smartEnv.fileloc + subpath;
		
		//濡傛灉鏂囦欢澶逛笉瀛樺湪鍒欏垱寤�  
		File file =new File(newPath);  
		if  (!file.exists())    
		{    
		    file.mkdir();  
		}
		
		String newFileName = upfile.getFileName();
		//鎵╁睍鍚�
		newFileName = UUID.randomUUID().toString().replaceAll("-", "") + newFileName.substring(newFileName.length()-5);
		
		String newfullname;
		newfullname = newPath + "\\" + newFileName;
		//System.out.println(newfullname);
		upfile.getFile().renameTo(new File(newfullname));
		
		//杩斿洖鐭枃浠跺悕
		return newFileName;
	}
	@NotAction
	public String upXlsxFile(String subpath) {
		//涓婁紶excel鏂囦欢鍒扮紦瀛樺尯鍩�
		UploadFile upfile = getFile();
		//璺緞
		String newPath = smartEnv.fileloc + subpath;
		
		//濡傛灉鏂囦欢澶逛笉瀛樺湪鍒欏垱寤�  
		File file =new File(newPath);  
		if  (!file.exists())    
		{    
		    file.mkdir();  
		}
		
		String newFileName = upfile.getFileName();
		//鎵╁睍鍚�
		newFileName = UUID.randomUUID().toString().replaceAll("-", "") + newFileName.substring(newFileName.length()-5);
		
		String newfullname;
		newfullname = newPath + "\\" + newFileName;
		//System.out.println(newfullname);
		upfile.getFile().renameTo(new File(newfullname));
		
		//杩斿洖鐭枃浠跺悕
		return newFileName;
	}
	@NotAction
	public void prepaerDir(String dirName) {
		File moveDir = new File(dirName);           // 鍒涘缓鐩爣鐩綍
        if (!moveDir.exists()) {                // 鍒ゆ柇鐩爣鐩綍鏄惁瀛樺湪
             moveDir.mkdirs();                     // 涓嶅瓨鍦ㄥ垯鍒涘缓
        }
	}
	@NotAction
	public String[] upFile(String subpath) {
		String[] fileNames = new String[2];
		//subpath鏄浉瀵箄pload鐨勫瓙鏂囦欢澶�, 杩斿洖鍊间负閲嶅懡鍚嶅悗鐨勫浘鐗囨枃浠跺悕
		UploadFile upfile = getFile();
		//璺緞  smartEnv.fileloc
		String newPath = smartEnv.fileloc + subpath;
		
		//System.out.println("Up loc is:" + newPath);
		
		//濡傛灉鏂囦欢澶逛笉瀛樺湪鍒欏垱寤�  
		File file =new File(newPath);  
		if  (!file.exists())    
		{    
		    file.mkdir();  
		}
		
		String orignalFileName = upfile.getFileName();
		fileNames[0] = orignalFileName;
		String extname = orignalFileName.substring(orignalFileName.lastIndexOf("."));
		
		//鎵╁睍鍚�
		String newFileName;
		newFileName = UUID.randomUUID().toString().replaceAll("-", "") + extname;
		
		String newfullname;
		newfullname = newPath + "\\" + newFileName;
		
		System.out.println(newfullname);
		upfile.getFile().renameTo(new File(newfullname));
		
		fileNames[1] = subpath + "/" + newFileName;
		return fileNames;
	}
}
